home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 228 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  75 lines

  1. Path: thor.tu.hac.com!collins
  2. From: collins@thor.tu.hac.com (Ron Collins)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Array Parameters
  5. Date: 3 Jan 1996 14:16:41 GMT
  6. Organization: Advanced Depot Systems
  7. Message-ID: <4ce349$4j9@hacgate2.hac.com>
  8. References: <wayne.820650643@hawk>
  9. NNTP-Posting-Host: thor.tu.hac.com
  10.  
  11. Wayne Elliott (wayne@adied.oz.au) wrote:
  12. : Can anyone fill me in on whats wrong with the following snippet of
  13. : code. Basically I'm trying to pass and use a multi-dimensional
  14. : array.
  15.  
  16. : -----------------------------------------------------------------
  17.  
  18. : /* Test passing of array parameters */
  19.  
  20. : #include <stdio.h>
  21.  
  22. : char map [][8] =
  23.   ^^^^^^^^^^^^^^^^
  24.  
  25. Change this to
  26.  
  27.    char *map[] =
  28. : {
  29. :   "abcdef",
  30. :   "ghijkl",
  31. :   "mnopqr"
  32. : };
  33.  
  34. : void test(int num, char ** maps)
  35.                      ^^^^^^^^^^^^
  36.  
  37. Change this to
  38.  
  39.   void test(int num, char*maps[])
  40. : {
  41. :   int i;
  42. :   for(i=0;i<num;i++)
  43. :     if(maps[i])
  44. :       printf("%04d %s\n", i, maps[i]);
  45. :     else
  46. :       printf("%04d NULL\n", i);
  47. : }
  48.  
  49. : int main()
  50. : {
  51. :   test(3, (char **) map);
  52.             ^^^^^^^^^
  53.  
  54. Change this to
  55.  
  56.     test(3, map);
  57. :   return 0;
  58. : }
  59.  
  60.  
  61. While there are circumstances where an array may behave as a pointer, they
  62. are different items with different characteristics and properties.  These
  63. differences become more pronounced with multi-dimensional arrays.  I believe
  64. the FAQ covers this in detail.
  65.  
  66.  
  67.                         -- Collins --
  68.                         
  69. -----
  70. The views expressed here are mine alone.
  71.  
  72. Ron Collins/Hughes Aircraft Company/M20,P20/Tucson Az 85706
  73. rcollins@thor.tu.hac.com    collins@seagull.rtd.com
  74. ยก----
  75.